package com.csoa.community.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.csoa.community.R;
import com.csoa.community.requests.LinkmanRequest;
import com.csoa.community.requests.RiChengRequest;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.NarView;

public class SettingActivity extends BaseActivity {

	private Button btnScreenBright;
	private String screenBright;
	private boolean isNightMode;
	private NarView narView;

	@Override
	public void initUI() {
		setContentView(R.layout.settings);
		initNarView();
		findViewById(R.id.st_tixing).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doTiXing();
			}
		});
		findViewById(R.id.st_tongbu).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doTongBu();
			}
		});
		
		findViewById(R.id.st_data_time).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doGetTimeData();
			}
		});
		findViewById(R.id.st_fujian).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				doFuJian();
			}
		});
		findViewById(R.id.st_clear).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						SettingActivity.this);
				builder.setTitle("确认删除附件缓存?");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								GlobalVars.getInst().clearAttachCache();
							}
						});
				builder.setNegativeButton("取消", null);
				builder.create().show();

			}
		});

		btnScreenBright = (Button) this.findViewById(R.id.st_screen_brightness);
		btnScreenBright.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if (isNightMode) {
					AndroidUtils.adjustScreenBright(SettingActivity.this, -1);
					AndroidUtils.setGlobal(GlobalVars.LOGIN,
							GlobalVars.LOGIN_CURRENT_SCREEAN_BRIGHT, "",
							SettingActivity.this);
					btnScreenBright.setText("夜间模式");
					isNightMode = false;

				} else {
					AndroidUtils.adjustScreenBright(SettingActivity.this);
					AndroidUtils.setGlobal(GlobalVars.LOGIN,
							GlobalVars.LOGIN_CURRENT_SCREEAN_BRIGHT, "1",
							SettingActivity.this);
					btnScreenBright.setText("日间模式");
					isNightMode = true;
				}

			}
		});

		screenBright = AndroidUtils.getGlobal(GlobalVars.LOGIN,
				GlobalVars.LOGIN_CURRENT_SCREEAN_BRIGHT, SettingActivity.this);

		if (screenBright != null && "1".equals(screenBright)) {
			isNightMode = true;
			btnScreenBright.setText("日间模式");

		} else {
			isNightMode = false;
			btnScreenBright.setText("夜间模式");
		}

		// 注销
		this.findViewById(R.id.set_btn_logout).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						new AlertDialog.Builder(SettingActivity.this)
								.setTitle("确实要注销当前用户吗?")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												logout();
											}

										}).setNegativeButton("取消", null).show();

					}
				});
	}
	
	private void initNarView() {
		narView = (NarView)this.findViewById(R.id.comNarView);
		narView.enterModule(R.id.nav_set);
	}

	private void logout() {
		Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
		intent.putExtra("logout", "logout");
		startActivity(intent);
		BaseActivity._activity.closeAllActivity();
		finish();
		GlobalVars.currentUser = null;
	}

	protected void doFuJian() {
		Intent intent = new Intent(this, FuJianActivity.class);
		startActivity(intent);
	}

	protected void doTongBu() {
		String[] items = new String[] { "一周", "两周", "四周" };
		int p = LinkmanRequest.getTongBuJianGe(SettingActivity.this);
		switch (p) {
		case 1:
			p = 0;
			break;
		case 2:
			p = 1;
			break;
		case 4:
		case 0:
			p = 2;
			break;
		}
		final AlertDialog ad = new AlertDialog.Builder(this)
				.setSingleChoiceItems(items, p,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								int date = 0;
								switch (which) {
								case 0:
									date = 1;
									break;
								case 1:
									date = 2;
									break;
								case 2:
									date = 4;
									break;
								default:
									break;
								}
								LinkmanRequest.setTongBuJianGe(
										SettingActivity.this, date);
								dialog.dismiss();
							}
						}).setTitle("设置同步时间间隔").create();
		ad.show();
	}

	protected void doTiXing() {
		String[] items = new String[] { "会议前半小时", "会议前一小时", "会议前一天", "取消提醒" };
		int p = RiChengRequest.getTiXingJianGe(SettingActivity.this);
		final AlertDialog ad = new AlertDialog.Builder(this)
				.setSingleChoiceItems(items, p,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								RiChengRequest.setTiXingJianGe(
										SettingActivity.this, which);
								dialog.dismiss();
							}
						}).setTitle("设置会议提醒").create();
		ad.show();
	}

	/**
	 * 获取指定时间内的数据（包括公文，工作等）
	 */
	protected void doGetTimeData() {
		String[] items = new String[] { "一周", "二周", "四周" };
		int p = getDataTime(SettingActivity.this);

		switch (p) {
		case 1:
			p = 0;
			break;
		case 2:
			p = 1;
			break;
		case 4:
			p = 2;
			break;
		}

		final AlertDialog ad = new AlertDialog.Builder(this)
				.setSingleChoiceItems(items, p,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								
								
								int date = 1;
								switch (which) {
								case 0:
									date = 1;
									break;
								case 1:
									date = 2;
									break;
								case 2:
									date = 4;
									break;
								default:
									break;
								}
								GlobalVars.DataCount = date * 7;
								setDataTime(SettingActivity.this, date);
								dialog.dismiss();
							}
						}).setTitle("获取数据设置").create();
		ad.show();
	}

	private final static String cache_file = "cache_file_get_data";
	private final static String cache_file_get_data_time = "cache_file_get_data_time";

	public static int getDataTime(Context cxt) {
		String str = AndroidUtils.getGlobal(cache_file,
				cache_file_get_data_time, cxt);
		if (StringUtils.isEmpty(str)) {
			setDataTime(cxt, 1);
			return 1;
		} else {
			return Integer.parseInt(str);
		}
	}

	public static void setDataTime(Context cxt, int type) {
		AndroidUtils.setGlobal(cache_file, cache_file_get_data_time, type + "",
				cxt);
	}

	@Override
	public void initData() {

	}

	public void startActivity(Class<?> activity) {
		Intent intent = new Intent(SettingActivity.this, activity);
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

}
